<?php
class User_DepartmentController extends Zend_Controller_Action
{
	/**
	* 部门列表 
	*
	*/
	private $_s;
	public function preDispatch(){
		$this->_s=new User_Service_Department();
	}	
	
	public function indexAction(){
			
		echo Cff::renderTpl('user/department/index.tpl',"layout");
	
	}	
	

	public function listAction(){
	
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$ud_supervisor_id=$this->_request->getParam('supervisor_id','');
		$ud_name=$this->_request->getParam('sname','');
		$intpage = ($intpage<1)?1:$intpage; 
		$condition=array(
			'ud_supervisor_id'=>$ud_supervisor_id
		);
		if($ud_name!=''){
			$condition=array(
				'ud_name'=>$ud_name
			);
		}
		$count=$this->_s->getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$Rows=$this->_s->getByCondition($condition,'*','ud_id',$pageSize,$intpage);		
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));

	}		
	
	public function createAction(){

	    $ud_name=$this->_request->getPost('departName');
		$ud_en_name=$this->_request->getPost('departEnName');
		$ud_supervisor_id=$this->_request->getPost('supervisor_id');
		$ud_sort=$this->_request->getPost('departSort');
		$ud_id=$this->_request->getPost('departId','');
		$data=0;
		$rows=0;
		$error=array();
		$return=array(
			'ask'=>0,
			'error'=>null,
		);
		$row=array(
			'ud_name'=>$ud_name,
			'ud_en_name'=>$ud_en_name,
			'ud_sort'=>$ud_sort,
			'ud_supervisor_id'=>$ud_supervisor_id
		);
		$error = $this->_s->validator($row);
        if(!empty($error)){
			$return=array(
				'ask'=>0,
				'error'=>$error,
			);
            die(Zend_Json::encode($return));
        }
		if(!empty($ud_id)){
			$inforows=$this->_s->getById($ud_id);	
			if(!empty($inforows)){		
				if($inforows['ud_name']==$ud_name && $inforows['ud_sort']==$ud_sort && $inforows['ud_supervisor_id']==$ud_supervisor_id ){
					$data=3;//没有任何修改
				}else{
					$rows=$this->_s->update($row,$ud_id);
					$data=2;//修改成功
				}
			}
			$return['ask']=$data;
		}else{
			$rows=$this->_s->getByName($ud_name);
			if(!empty($rows)){
				$error[]="Add Fail ".$ud_name." Exists!";
				$return['ask']=0;
				$return['error'] =$error;
				die(Zend_Json::encode($return));
			}
			$create=$this->_s->create($row);
			if($create>0){
				$return['ask']=1;//添加成功
			}
		}
		die(Zend_Json::encode($return));
	}

	public function infoAction(){
		$ud_id=$this->_request->getParam('ud_id');
		$rows=array();
		if(!empty($ud_id)){
			$rows=$this->_s->getById($ud_id);
		}
		die(Zend_Json::encode($rows));
	}	
	
	public function deleteAction(){
		$ud_id=$this->_request->getPost('ud_id');
		$rows=0;
		if(!empty($ud_id)){
			$rows=$this->_s->delete($ud_id);
		}
		if($rows>0){
			$massage['data']=1;
		}else{
			$massage['data']=0;
		}
		die(Zend_Json::encode($massage));
	}
	
	
	public function selectAction(){
	
		$rows=$this->_s->getAll();
		die(Zend_Json::encode($rows));
	}	
	
	
}